package com.study.designpartten.proxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * 动态代理
 */
public class MyPoxy implements InvocationHandler {

    private Object target;

    public MyPoxy(Object target){
        this.target = target;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        System.out.println("start----");
        Object o = method.invoke(target,args);
        System.out.println("end------");

        return o;
    }

    public static void main(String[] args) {
        Subject subject = new RealSubject();
        MyPoxy m = new MyPoxy(subject);
        Subject proxySubect = (Subject)Proxy.newProxyInstance(subject.getClass().getClassLoader(),subject.getClass().getInterfaces(),m);
        Subject proxySubect2 = DynamicProxy.newProxyInstance(subject.getClass().getClassLoader(),subject.getClass().getInterfaces(),m);
        System.out.println(proxySubect.doSomething(1,2));
        System.out.println(proxySubect2.doSomething(3,4));
    }

}
